//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#pragma once

#include "WeInputDeviceHandler.h"
#include "WeVector2.h"

namespace WackiEngine
{
	// holds status of device buttons and axes
	struct DeviceStatus
	{
		// did the status change this frame?
		bool	changed;
		// is this device connected
		bool	connected;
		// deviceId for this device in the engine
		uint32	deviceId;

		// map of axes states
		Map<KeyCode, float> axes;
		// button pressed states
		Map<KeyCode, bool> prevButtons;
		Map<KeyCode, bool> buttons;
	};


	class XInputDH : public InputDeviceHandler
	{
	public:
		XInputDH();

		void update();

	private:
		// connected controllers (XInput allows for a max of 4? I believe)
		DeviceStatus _controllers[4];

		// current controller index in the update
		int32		_currentIndex;

		void		checkStatus();
		void		updateAxes();
		void		updateVirtualButtons();
		void		updateButtons();
		void		generateEvents();
		
		// updates two virtual buttons based on an axis that has a range from -X to +X where X >= threshold
		void		updateVirtualButton(KeyCode axis, KeyCode buttonNegative, KeyCode buttonPositive, float threshold);

		// updates a virtual button corresponding to an axis value
		void		updateVirtualButton(KeyCode axis, KeyCode button, float threshold);

		// sets the status of a button
		void		setButton(KeyCode button, bool value);
		bool		getButton(KeyCode button);
		bool		getButtonPrev(KeyCode button);
	};

}